package com.mail.server.controller;


import com.mail.model.db.PmsBrand;
import com.mail.model.vo.PageResult;
import com.mail.server.common.R;
import com.mail.server.service.PmsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author WJZ
 * @since 2022-06-20
 */
@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService brandService;

    /**
     * 获取全部品牌列表
     *
     * @return
     */
    @GetMapping("/listAll")
    public R listAll() {
        List<PmsBrand> list = brandService.listAll();
        return R.success(list);
    }

    /**
     * 根据品牌名称分页获取品牌列表
     *
     * @param keyword：品牌名称对应品牌实体中name属性
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer pageNum,
                  @RequestParam(defaultValue = "1") Integer pageSize,
                  String keyword) {
        PageResult result = brandService.list(pageNum, pageSize, keyword);
        return R.success(result);
    }

    /**
     * 根据编号查询品牌信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R query(@PathVariable Long id) {
        PmsBrand brand = brandService.query(id);
        return R.success(brand);
    }

    /**
     * 更新品牌
     *
     * @param id
     * @param brand
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateBrand(@PathVariable Long id, @RequestBody PmsBrand brand) {
        Integer count = brandService.updateBrand(id, brand);
        if (count == null) {
            return R.error("更新品牌失败");
        }
        return R.success("更新品牌成功");
    }

    /**
     * 批量更新显示状态
     *
     * @param ids
     * @param showStatus
     * @return
     */
    @PostMapping("/update/showStatus")
    public R updateShowStatus(@RequestParam List<Long> ids, Integer showStatus) {
        brandService.updateShowStatus(ids, showStatus);
        return R.success("更新状态成功");
    }

    /**
     * 批量更新厂家制造商状态
     *
     * @param ids
     * @param factoryStatus
     * @return
     */
    @PostMapping("/update/factoryStatus")
    public R updateFactoryStatus(@RequestParam List<Long> ids, Integer factoryStatus) {
        brandService.updateFactoryStatus(ids, factoryStatus);
        return R.success("更新状态成功");
    }

    /**
     * 批量删除品牌
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete/batch")
    public R deleteBatch(@RequestParam List<Long> ids) {
        Integer count = brandService.deleteBatch(ids);
        if (count == null) {
            return R.error("删除失败");
        }
        return R.success("删除成功");
    }

    /**
     * 删除指定品牌
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Long id) {
        Integer count = brandService.deleteBrand(id);
        if (count == null) {
            return R.error("删除失败");
        }
        return R.success("删除成功");
    }

    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody PmsBrand brand) {
        Integer count = brandService.create(brand);
        if (count == null) {
            return R.error("添加失败");
        }
        return R.success("天剑成功");
    }
}
